import dayjs from 'dayjs'
import customParseFormat from 'dayjs/plugin/customParseFormat'
import advancedFormat from 'dayjs/plugin/advancedFormat'
import quarterofyear from 'dayjs/plugin/quarterOfYear'
import weekofyear from 'dayjs/plugin/weekOfYear'
dayjs.extend(customParseFormat)
dayjs.extend(advancedFormat)
dayjs.extend(quarterofyear)
dayjs.extend(weekofyear)

export {
  dayjs,parseDayjs
}

function parseDayjs(value: string, format: string){
  if (['YYYY-MM-DD', 'YYYY-MM', 'YYYY'].includes(format)) {
    return dayjs(value);
  } else if ('YYYY-ww' === format) {
    const tmp = value.split('-');
    return dayjs(tmp[0]).week(Number(tmp[1]))
  } else if ('YYYY-Q' === format) {
    const tmp = value.split('-');
    return (dayjs(tmp[0]) as any).quarter(Number(tmp[1]))
  }
}